<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_robshop/model')->方法名();
*/
namespace jy_robshop;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function gethourkey(){
		//获取当前时间：
		$showtypetime1=mktime(12,00,0,date('m'),date('d'),date('Y'));
		$showtypetime2=mktime(18,00,0,date('m'),date('d'),date('Y'));
		$showtypetime3=mktime(24,00,0,date('m'),date('d'),date('Y'));
		if(TIMESTAMP<$showtypetime1){
			$hourkey=0;
		}elseif(TIMESTAMP>$showtypetime1 && TIMESTAMP<$showtypetime2){
			$hourkey=1;
		}elseif(TIMESTAMP>$showtypetime2 && TIMESTAMP<$showtypetime3){
			$hourkey=2;
		}
		return $hourkey;
	}
	public function up_canlog($item){
		$hourkey=$this->gethourkey();
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'goodsid'=>$item['goodsid'],
					'fee'=>$item['fee'],
					'hourkey'=>$hourkey,
					'dayid'=>date('Y-m-d'),
					'sendtype'=>$item['sendtype'],
					'address'=>$item['address'],
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('canlog',$data);
		$canlogid=pdo_insertid();
		return $canlogid;
	}
	public function up_yuelog($item){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'goodsid'=>$item['goodsid'],
					'buynum'=>$item['buynum'],
					'buydays'=>$item['buydays'],
					'yue_fee'=>$item['yue_fee'],
					'yue_quan'=>$item['yue_quan'],
					'goodsfee'=>$item['goodsfee'],
					'allfee'=>$item['allfee'],
					'allnum'=>$item['allnum'],
					'sendtype'=>$item['sendtype'],
					'address'=>$item['address'],
					'starttime'=>$item['starttime'],
					'endtime'=>time()+($item['buydays']-1)*24*3600+3600,
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('yuelog',$data);
		$yuelogid=pdo_insertid();
		return $yuelogid;
	}
	public function yuelog_pay($yuelog,$params=array()){
		$update=array(
			'paytype'=>$params['paytype'],
			'transid'=>$params['transid'],
			'ispay'=>1,
			'status'=>1,
		);
		$this->pdo_update('yuelog',$update,array('id'=>$yuelog['id']));
		$quanlog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'goodsid'=>$yuelog['goodsid'],
			'yuelogid'=>$yuelog['id'],
			'num'=>$yuelog['yue_quan'],
			'type'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('quanlog',$quanlog);
		model('core/member')->member_credit($_SESSION['uid'],'credit4',$yuelog['yue_quan'],false,'jy_robshop','预约抢购');
		$yuelog['fee']=$yuelog['allfee'];
		$yuelog['yuelogid']=$yuelog['id'];
		$this->add_feelog($yuelog,6);
		return true;
	}
	public function canlog_pay($canlog,$params=array()){
		$update=array(
			'paytype'=>$params['paytype'],
			'transid'=>$params['transid'],
			'ispay'=>1,
			'status'=>1,
		);
		$this->pdo_update('canlog',$update,array('id'=>$canlog['id']));
		$canlog['canlogid']=$canlog['id'];
		$this->add_feelog($canlog,7);
		if($this->setting['upteam_type']){
			$goods=$this->get('goods',array('id'=>$canlog['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			$this->up_can2man($goods);
		}
		$member=$this->get('member',array('mid'=>intval($canlog['mid'])),'fromuser');
		if($member['fromuser']){
			//名下总业绩N元
			$this->model('team')->update_agent_level($member['fromuser']);
		}
		return true;
	}
	public function up_yue2canlog($goods){
		$hourkey=$this->gethourkey();
		$yuelogs=$this->getall('yuelog',array('goodsid'=>$goods['id'],'ispay'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
		$daytime=strtotime(date('Y-m-d'));
		foreach($yuelogs as $yv){
			$isdaynum=$this->count('canlog',array('createtime >'=>$daytime,'yuelogid'=>$yv['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($isdaynum){
				continue;
			}
			$isallnum=$this->count('canlog',array('yuelogid'=>$yv['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($isallnum>=$yv['allnum']){
				$this->pdo_update('yuelog',array('status'=>2),array('id'=>$yv['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']));
				continue;
			}
			$upnum=$yv['allnum']-$isallnum>$yv['buynum']?$yv['buynum']:$yv['allnum']-$isallnum;
			for($i=1;$i<=$upnum;$i++){
				$data=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$yv['mid'],
							'goodsid'=>$yv['goodsid'],
							'fee'=>$goods['price'],
							'yuelogid'=>$yv['id'],
							'sendtype'=>$yv['sendtype'],
							'address'=>$yv['address'],
							'hourkey'=>$hourkey,
							'dayid'=>date('Y-m-d'),
							'createtime'=>TIMESTAMP,
							'ispay'=>1,
							'status'=>1,
				);
				$this->insert('canlog',$data);
				if($this->setting['upteam_type']){
					$this->up_can2man($goods);
				}
			}
			if($isallnum+$upnum>=$yv['allnum']){
				$this->pdo_update('yuelog',array('status'=>2),array('id'=>$yv['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']));
			}
			$member=$this->get('member',array('mid'=>intval($yv['mid'])),'fromuser');
			if($member['fromuser']){
				////名下总业绩N元
				$this->model('team')->update_agent_level($member['fromuser']);
			}
		}
		return true;
	}
	//满人成团
	public function up_can2man($goods=array()){
		$params=array('goodsid'=>$goods['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']);
		$teamnum=$this->count('canlog',$params);
		if($teamnum>=$goods['jian_teamnum']){
			$pindex = max(1, intval($_RQ['page']));
			$psize = $goods['jian_teamnum'];
			$orderby='createtime asc';
			$total='';
			$canlogs=pdo_getslice('jy_robshop_canlog',$params,array($pindex,$psize),$total,$fields,'',$orderby);
			$allfee=$goods['jian_teamnum']*$canlogs[0]['fee'];
			for($i=1;$i<=$goods['zhongnum'];$i++){
					$key=mt_rand(0,$goods['jian_teamnum']-$i);
					if(!$canlogs[$key]['id']){
						$key=0;
					}
					$this->canlog2($canlogs[$key],$goods);
					unset($canlogs[$key]);
					$canlogs=array_values($canlogs);
			}
			$failcanlogs=$canlogs;
			if(count($failcanlogs)){
				$onefee=$failcanlogs[0]['fee']/count($failcanlogs);
			}
			$isgetred=0;
			if($goods['jian_redmax']>0){
				//$maxredfee=$goods['zhongnum']*$canlogs[0]['fee'];
				//$maxredfee=$maxredfee*$goods['jian_redmax']/100;
			}
			$failnum=count($failcanlogs);
			foreach($failcanlogs as $nowkey=>$item){
				$redfee=$this->canlog3($item,$goods,$isgetred,$maxredfee,$failnum,$nowkey);
				if($redfee>0){
					$isgetred+=$redfee;
				}
			}
			//公益池
			$params['dayid']=date('Y-m-d');
			$this->gongfeelog($params,$allfee);
		}
		return true;		
	}
	//抢购结算
	public function up_can2success($goods,$lasttime=0){
		$day=date('Y-m-d');
		$hourkey=$this->gethourkey();
		if($goods['jian_openday']){
			$start_time=strtotime($day)-24*3600;
			$end_time=strtotime($day);
			$nowday=date('Y-m-d',$start_time+1000);
		}else{
			$nowhours=json_decode($goods['jian_hour'.$hourkey],true);
			$end_time=strtotime($day.' '.$nowhours['end']);
			if($end_time<time()){
				$lasthourkey=$hourkey;
				$nowday=$day;
			}else{
				if($hourkey){
					$lasthourkey=$hourkey-1;
					$nowday=$day;
				}else{
					$lasthourkey=2;
					$daytime=strtotime($day)-3600;
					$nowday=date('Y/m/d',$daytime);
				}
			}
			$goods['hours']=json_decode($goods['jian_hour'.$lasthourkey],true);
			$start_time=strtotime($nowday.' '.$goods['hours']['start']);
			$end_time=strtotime($nowday.' '.$goods['hours']['end']);
		}
		$params=array('goodsid'=>$goods['id'],'dayid'=>$nowday,'hourkey'=>$lasthourkey,'status'=>1,'uniacid'=>$_SESSION['uniacid']);
		if($goods['jian_openday']){
			unset($params['hourkey']);
		}
		//$params['createtime between']=array($start_time,$end_time);
		$canlogs=$this->getall('canlog',$params);
		$num=count($canlogs);
		if(!$num){
			return false;
		}
		$allfee=$num*$canlogs[0]['fee'];
		if($num && $goods['jian_teamnum']){
			$teamnum=intval($num/$goods['jian_teamnum']);
			if($teamnum>0){
				$zhongnum=$teamnum*$goods['zhongnum'];
				for($i=1;$i<=$zhongnum;$i++){
					$key=mt_rand(0,$num-$i);
					if(!$canlogs[$key]['id']){
						$key=0;
					}
					$this->canlog2($canlogs[$key],$goods);
					unset($canlogs[$key]);
					$canlogs=array_values($canlogs);
				}
			}
		}
		$failcanlogs=$this->getall('canlog',$params);
		if(count($failcanlogs)){
			$onefee=$failcanlogs[0]['fee']/count($failcanlogs);
		}
		$isgetred=0;
		if($goods['jian_redmax']>0){
			//$maxredfee=$goods['zhongnum']*$canlogs[0]['fee'];
			//$maxredfee=$maxredfee*$goods['jian_redmax']/100;
		}
		$failnum=count($failcanlogs);
		foreach($failcanlogs as $nowkey=>$item){
			$redfee=$this->canlog3($item,$goods,$isgetred,$maxredfee,$failnum,$nowkey);
			if($redfee>0){
				$isgetred+=$redfee;
			}
		}
		//公益池
		$this->gongfeelog($params,$allfee);
		return true;
	}
	public function canlog2($canlog,$goods=array()){
		$goodsid=$goods['goodsid'];
		if(!$goods['goodstype']){
			$shopgoods[]=array(
				'goodsid'=>$goodsid,
				'goodsnum'=>1,
				'price'=>$canlog['fee'],
				'total'=>1,
				'plugin_name'=>'jy_robshop',
			);
			$canlog['address']=json_decode($canlog['address'],true);
			$new_order=array(
				'mid'=>$canlog['mid'],
				'price'=>$canlog['fee'],
				'goodsprice'=>$canlog['fee'],
				'remark'=>$canlog['remark'],
				'sendtype'=>$canlog['sendtype'],
				'realname'=>$canlog['address']['name'],
				'mobile'=>$canlog['address']['mobile'],
				'address'=>$canlog['address'],
				'plugin_name'=>'jy_robshop',
				'is_team'=>1,
				'teamid'=>$canlog['id'],
				'goods'=>$shopgoods,
			);
			if(!$new_order['merchid'] && $_SESSION['onemerchid']){
				$new_order['api_data']=array('dhmerchid'=>$_SESSION['onemerchid']);
			}
			$order_id=model('jy_weishop/order')->create($new_order);
			pdo_update('jy_weishop_order',array('status'=>1,'ispay'=>1,'paytype'=>$canlog['paytype'],'transid'=>$canlog['transid'],'paytime'=>time()),array('id'=>$order_id));
			$this->pdo_update('canlog',array('orderid'=>$order_id),array('id'=>$canlog['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			model('core/member')->member_credit($canlog['mid'],'credit6',$goods['creditnum'],true,'jy_robshop','抢中得'.$this->btitle);
			$canlog['fee']=$goods['creditnum'];
			$this->add_feelog($canlog,8);
		}
		$this->pdo_update('canlog',array('status'=>2),array('id'=>$canlog['id'],'uniacid'=>$_SESSION['uniacid']));
		$this->model('team')->createfee($canlog);
		//大转盘等级分润
		$this->redfeelog($canlog);
		if($goods['zhong_jf']>0){
			$jifen=$goods['zhong_jf'];
			model('core/member')->member_credit($canlog['mid'],'credit1',$jifen,true,'jy_robshop','抢中得积分');
			$canlog['fee']=$jifen;
			$this->add_feelog($canlog,3);
		}
		//抢中获得大转盘
		$gamepost=array(
			'type'=>1,
			'price'=>$canlog['fee'],
		);
		$this->model('game')->up_addgame($gamepost,$canlog['mid']);
		//抢购抢中N元
		$this->model('team')->update_agent_level($canlog['mid']);
		return true;
	}
	public function canlog3($canlog,$goods=array(),$isgetredfee=0,$maxredfee=0,$failnum=1,$nowkey=0){
		$goods=$goods?:pdo_get('jy_robshop_goods',array('goodsid'=>$canlog['id'],'uniacid'=>$_SESSION['uniacid']));
		//已去除
		if($goods['jian_redmax'] && false){
			if($maxredfee>0){
				if($nowkey+1==$failnum){
					$redfee=$maxredfee-$isgetredfee;
				}else{
					$rand=mt_rand(1,100);
					$max=2*($maxredfee/$failnum);
					$redfee=($max*$rand)/100;
					$lastredfee=$maxredfee-$isgetredfee;
					if($lastredfee<$redfee){
						$redfee=$lastredfee;
					}
				}
			}
			if($redfee>0){
				//注销，移步领红包
				//model('core/member')->member_credit($canlog['mid'],'credit2',$redfee,true,'jy_robshop','抢购失败获得红包');
				$data=$canlog;
				$data['canlogid']=$canlog['id'];
				$data['fee']=$redfee;
				$this->add_feelog($data,2);
				$this->model('team')->up_red_per($canlog,$redfee);
			}
		}
		model('core/member')->member_credit($canlog['mid'],'credit2',$canlog['fee'],true,'jy_robshop','抢购失败退款');
		$this->pdo_update('canlog',array('status'=>3,'tuitime'=>TIMESTAMP),array('id'=>$canlog['id'],'uniacid'=>$_SESSION['uniacid']));
		$this->add_feelog($canlog,1);
		return $redfee;
	}
	public function check_buy($goods,$post=array(),$isyue=false,$member=array()){
		if(!$isyue){
			
		}else{
			$yue_buynum=$this->setting['yue_buynum'];
			$yue_days=$this->setting['yue_days'];
			if($yue_buynum && $yue_buynum<$post['yue_buynum']){
				exi('每天之多抢购'.$yue_buynum.'次！','error');
			}
			if($yue_days && $yue_days<$post['yue_days']){
				exi('一共最多预约抢购'.$yue_days.'天！','error');
			}
			$isyue_days=pdo_getsum('jy_robshop_yuelog',array('status'=>1,'mid'=>$_SESSION['uid'],'goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'buydays');
			if($yue_days && $yue_days<$post['yue_days']+$isyue_days){
				exi('一共最多预约抢购'.$yue_days.'天！您之前已预约'.$isyue_days.'天！','error');
			}
			$yue_fee=$this->setting['yue_fee']?:0;
			$yue_quan=$this->setting['yue_quan']?:0;
			if($yue_quan){
				$allyue_quan=$yue_quan*$post['yue_buynum']*$post['yue_days'];
				if($allyue_quan>$member['credit4']){
					exi('本次预约消耗'.$allyue_quan.'个兑换券！当前账户兑换券不足！','error');
				}
			}
		}
		return true;
	}
	public function add_feelog($item,$type=0){
		$hourkey=$this->gethourkey();
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'goodsid'=>$item['goodsid'],
					'canlogid'=>$item['canlogid']?:$item['id'],
					'yuelogid'=>$item['yuelogid'],
					'fee'=>$item['fee'],
					'hourkey'=>$hourkey,
					'dayid'=>date('Y-m-d'),
					'type'=>$type,
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('feelog',$data);
		return true;
	}
	public function redfeelog($canlog=array()){
		$game=pdo_get('jy_weishop_game',array('action'=>'jy_robshop/game/','plugin_name'=>'jy_robshop','uniacid'=>$_SESSION['uniacid']));
		$game['reward']=$game['reward']?json_decode($game['reward'],true):array();
		if(!$game['reward'] || !$game['id']){
			return false;
		}
		$allfee=$canlog['fee'];
		foreach($game['reward'] as $k=>$item){
			if(!$item['redfee_rate'] || $item['redfee_rate']<=0){
				continue;
			}
			$fee=$allfee*$item['redfee_rate']/100;
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'canlogid'=>$canlog['id'],
				'goodsid'=>$canlog['goodsid'],
				'allfee'=>$allfee,
				'fee'=>$fee,
				'hourkey'=>$canlog['hourkey'],
				'dayid'=>$canlog['dayid'],
				'reward_key'=>$k,
				'createtime'=>TIMESTAMP,
				'status'=>0,
			);
			$this->insert('redfeelog',$data);
		}
		return true;
	}
	public function gongfeelog($params,$allfee=0){
		$gong_fee=$this->setting['gong_fee'];
		if(!$gong_fee || $allfee<=0){
			return false;
		}
		$fee=$allfee*$gong_fee/100;
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'goodsid'=>$params['goodsid'],
			'allfee'=>$allfee,
			'fee'=>$fee,
			'hourkey'=>$params['hourkey'],
			'dayid'=>$params['dayid'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		$this->insert('gongfeelog',$data);
		return true;
	}
	public function team_level_up_types(){
		$teamsetting=$this->plugin_setting('teamsetting');
		$set=str_array($teamsetting['level_up_types']);
		$level_up_types=$this->level_up_types;
		foreach($set as $k=>$type){
			if(!$level_up_types[$type]){
				unset($set[$k]);
			}
		}
		return $set;
	}
	public function current_level_up_types(){
		$areasetting=$this->plugin_setting('areasetting');
		return str_array($areasetting['level_up_types']);
	}
}
?>